﻿using _15Net8AspNetCore_ORM_Dapper.Context;
using _15Net8AspNetCore_ORM_Dapper.Models;
using Dapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace _15Net8AspNetCore_ORM_Dapper.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        private DapperContext _DapperContext;

        public ProductController(DapperContext dapperContext)
        {
            _DapperContext = dapperContext;
        }


        /// <summary>
        /// 新增商品
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> AddProduct(Product product)
        {
            // 1、创建链接
            using (var connect = _DapperContext.DbConnection())
            {
                //int id = await connect.ExecuteAsync("insert into products (name) values (@name) ", new { name = product.Name });  // 获取插入的个数
                int id = await connect.ExecuteScalarAsync<int>("insert into products (name) values (@name) ; select LAST_INSERT_ID(); ", new { name = product.Name });  // 获取插入的个数

                return CreatedAtAction(nameof(GetProduct), new { id = id }, product);
            }
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        [HttpPost("id")]
        public async Task<IActionResult> DeleteProduct(int id)
        {
            // 1、创建链接
            using (var connect = _DapperContext.DbConnection())
            {
                await connect.ExecuteAsync("delete from products where id = @Id ", new { Id = id });

                return CreatedAtAction(nameof(GetAllProduct), new Product());
            }
        }

        /// <summary>
        /// 修改商品
        /// </summary>
        [HttpPut("id")]
        public async Task<IActionResult> UpdateProduct(int id, Product product)
        {
            // 1、创建链接
            using (var connect = _DapperContext.DbConnection())
            {
                await connect.ExecuteAsync("update products set name = @name where id = @Id ", new { name = product.Name, Id = id });

                return CreatedAtAction(nameof(GetProduct), new { id = id }, product);
            }
        }

        /// <summary>
        /// 获取商品
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetAllProduct()
        {
            // 1、创建链接
            using (var connect = _DapperContext.DbConnection())
            {
                List<Product> products = connect.Query<Product>("Select * from products").ToList();

                return Ok(products);
            }
        }

        /// <summary>
        /// 获取商品
        /// </summary>
        [HttpGet("id")]
        public async Task<IActionResult> GetProduct(int id)
        {
            // 1、创建链接
            using (var connect = _DapperContext.DbConnection())
            {
                Product product = await connect.QueryFirstAsync<Product>("Select * from products where id = @Id ", new { Id = id });

                return Ok(product);
            }
        }
    }
}
